<?php

namespace addons\xiluedu;

use app\common\library\Menu;
use app\common\model\xiluedu\CourseOrder;
use app\common\model\xiluedu\OfflineOrder;
use app\common\model\xiluedu\UserMessage;
use app\common\model\xiluedu\UserMessageAccount;
use app\common\model\xiluedu\VipOrder;
use think\Addons;

/**
 * 插件
 */
class Xiluedu extends Addons
{

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        $menu = include ADDON_PATH . 'xiluedu' . DS . 'data' . DS . 'menu.php';
	    Menu::create($menu);
        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        Menu::delete("xiluedu");
        return true;
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {
        Menu::enable("xiluedu");
        return true;
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {
        Menu::disable("xiluedu");
        return true;
    }

    /**
     * 线上课程购买成功
     */
    public function xilueduCourseBuy(CourseOrder &$courseOrder) {
        if(!$courseOrder || $courseOrder->pay_status == 1) {
            //不存在或未支付
            return;
        }
        $model = UserMessage::create([
            'user_id' => $courseOrder->user_id,
            'type' => UserMessage::TYPE_COURSE_ORDER,
            'title' => '您已成功购买在线视频！',
            'content' => sprintf('您已成功购买了%s,请前往学习吧~',$courseOrder->course->name),
            'extra' => ['order_id'=>$courseOrder->id,'course_id'=>$courseOrder->course_id]
        ]);
        UserMessageAccount::where('user_id',$courseOrder->user_id)->setInc('user_message',1);
    }

    /**
     * 线下课程购买成功
     */
    public function xilueduOfflineBuy(OfflineOrder &$courseOrder) {
        if(!$courseOrder || $courseOrder->pay_status == 0) {
            //不存在或未支付
            return;
        }
        $model = UserMessage::create([
            'user_id' => $courseOrder->user_id,
            'type' => UserMessage::TYPE_OFFLINE_ORDER,
            'title' => '您已成功预约线下课程！',
            'content' => sprintf('您已预约了%s,请及时参与哦~',$courseOrder->course_name),
            'extra' => ['order_id'=>$courseOrder->id,'course_id'=>$courseOrder->course_id]
        ]);
        UserMessageAccount::where('user_id',$courseOrder->user_id)->setInc('user_message',1);
    }

    /**
     * 线下Vip购买成功
     */
    public function xilueduVipBuy(VipOrder &$vipOrder) {
        if(!$vipOrder || $vipOrder->pay_status == 0) {
            //不存在或未支付
            return;
        }
        $model = UserMessage::create([
            'user_id' => $vipOrder->user_id,
            'type' => UserMessage::TYPE_VIP_ORDER,
            'title' => '您已成功购买vip！',
            'content' => sprintf('您已成功开通%s会员,请前往学习吧~',$vipOrder->vip_name),
            'extra' => ['order_id'=>$vipOrder->id,'vip_id'=>$vipOrder->vip_id]
        ]);
        UserMessageAccount::where('user_id',$vipOrder->user_id)->setInc('user_message',1);
    }


    /**
     * 应用初始化
     */
    public function appInit()
    {
        //先判断是否已经通过其它方式引入了此类
        if(!class_exists("\PHPQRCode\QRcode")){
            \think\Loader::addNamespace('PHPQRCode', ADDON_PATH . 'xiluedu' . DS . 'library' . DS . 'PHPQRCode' . DS.'lib'.DS.'PHPQRCode'.DS);
        }
    }

}
